<?php
/**
 * Implementation of PHP Speedy for Joomla
 * @copyright www.fijiwebdesign.com
 * @author gabe@fijiwebdesign.com
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, http://aciddrop.com/php-speedy/
 */

// included only
if( !(defined( '_VALID_MOS' ) || defined( '_JEXEC' )) ) die( 'Direct Access to this location is not allowed.' );

global $mainframe;

if( get_class($mainframe) != 'jadministrator' ) {
	if( defined('_JEXEC') ) {
		$mainframe->registerEvent( 'onAfterInitialise', 'bot_speedy_start' );
		
	} else {
		$_MAMBOTS->registerFunction( 'onAfterStart', 'bot_speedy_start' );
	}
}

/**
* Include Speedy Class Files and start speedy
*/
function bot_speedy_start() {
	global $database, $mainframe, $compressor;

	$params = speedy_getParams();
	
	// check if debug mode is on and not debugging
	if (!mosGetParam($_GET, 'speedy') && $params->get('debug_mode')) {
		return;
	}
	
	// the executing component name
	$option = mosGetParam($_REQUEST, 'option', false);
	
	// don't allow compression for these components
	$not_options = array(
		'login',
		'logout',
		'com_logout',
		'com_login',
		'com_registration'
	);
	$disallowed_options = $params->get('disallowed_options', false);
	if ($disallowed_options) {
		$disallowed_options = explode("\n", $options);
		array_map('trim', $disallowed_options);
		$not_options = array_merge($not_options, $disallowed_options);
	}
	if (in_array($option, $not_options)) {
		$mainframe->addCustomHeadTag('<!-- PHP Speedy disabled for: '.htmlentities($option).'-->');
		return;
	}
	
	// only allow these components
	$options = $params->get('allowed_options', false);
	if ($options) {
		$allow = false;
		$options = explode("\n", $options);
		array_map('trim', $options);
		if (!in_array($option, $options)) {
			$mainframe->addCustomHeadTag('<!-- PHP Speedy not enabled for: '.htmlentities($option).'-->');
			return;
		}
	}
	
	$mainframe->addCustomHeadTag('<!-- optimized by Joomla PHP Speedy, Fiji Web Design. -->');
	
	// get the base path
	$base_path = $mainframe->getCfg('absolute_path');

	//We need these
	require($base_path.'/mambots/system/php_speedy/controller/compressor.php');
	require($base_path.'/mambots/system/php_speedy/libs/php/view.php'); //Include this for path getting help
	//chdir($cwd);
	
	//We need to know the config
	#########################################
	## Compressor option file ##############
	#########################################
	## Access control
	//$compress_options['username'] = "21232f297a57a5a743894a0e4a801fc3";
	//$compress_options['password'] = "4fe705bb8b349da9588e746cda9dbe2d";
	## Path info
	$compress_options['document_root'] = $_SERVER['DOCUMENT_ROOT'];
	$compress_options['javascript_cachedir'] = $base_path.'/mambots/system/php_speedy/cache/js/';
	$compress_options['css_cachedir'] = $base_path.'/mambots/system/php_speedy/cache/css/';
	## Minify options
	$compress_options['minify']['javascript'] = $params->get('minify_js', '0');
	$compress_options['minify']['page'] = $params->get('minify_page', '0');
	$compress_options['minify']['css'] = $params->get('minify_css', '0');
	## Gzip options
	$compress_options['gzip']['javascript'] = $params->get('gzip_js', '0');
	$compress_options['gzip']['page'] = $params->get('gzip_page', '0');
	$compress_options['gzip']['css'] = $params->get('gzip_css', '0');
	## Versioning
	$compress_options['far_future_expires']['javascript'] = $params->get('far_future_expires_js', '0');
	$compress_options['far_future_expires']['css'] = $params->get('far_future_expires_css', '0');
	#########################################
	$compress_options['show_timer'] = $params->get('show_timer', '0');
	$hide_errors = $params->get('hide_errors', '0');

	//Con. the view library
	$view = new compressor_view();
	
	//Con. the js min library
	if(substr(phpversion(),0,1) == 5) {
		require_once($base_path.'/mambots/system/php_speedy/libs/php/jsmin.php');
		$jsmin = new JSMin(null);
	}
	
	//Con. the compression controller
	$compressor = new compressor(
		array('view'=>$view,
		   'options'=>$compress_options,
		   'jsmin'=>$jsmin)
	 );
	if ($hide_errors) {
		error_reporting(0);
	}

	// clean current buffer
	$content = ob_get_contents();
	ob_end_clean();
	// restart buffering, with our callback
	ob_start(array($compressor, 'finish'));
	echo $content;

}

/**
* Retrieve plugin params
*/
function speedy_getParams() {
	global $database, $_MAMBOTS;

	if ( !isset($_MAMBOTS->_content_mambot_params['bot_speedy']) ) {
		// load mambot params info
		$query = "SELECT params"
		. "\n FROM #__mambots"
		. "\n WHERE element = 'bot_speedy'"
		. "\n AND folder = 'system'"
		;
		$database->setQuery( $query );
		$database->loadObject($mambot);

		// save query to class variable
		$_MAMBOTS->_content_mambot_params['bot_speedy'] = $mambot;
	}

	$mambot = $_MAMBOTS->_content_mambot_params['bot_speedy'];

 	$botParams = new mosParameters( $mambot->params );
	return $botParams;
}

?>
